﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Formatting;
using MyCities.Models;

namespace MyCities
{
    public partial class TestPage : PhoneApplicationPage
    {
        //// URI for RESTful service (implemented using Web API)
        //private const String serviceURI = "http://mycity.azurewebsites.net/city/dublin";

        public TestPage()
        {
            InitializeComponent();
        }

        private async void GetDataClick(object sender, RoutedEventArgs e)
        {
            try
            {
                //// creating system progress indicator
                //SystemTray.ProgressIndicator = new ProgressIndicator();

                //// turning on progress indicator
                //SetProgressIndicator(true);

                //// show running progress bar
                //SystemTray.ProgressIndicator.Text = "Connecting";

                TestProgressBar.Visibility = System.Windows.Visibility.Visible;

                // disable button
                GetDataButton.IsEnabled = false;

                //HttpClient client = new HttpClient();
                //client.BaseAddress = new Uri("http://mycity.azurewebsites.net");

                //// set JSON as default header
                //client.DefaultRequestHeaders.Accept.Add(
                //new MediaTypeWithQualityHeaderValue("application/json"));

                //// Get request to get city object for Dublin
                //HttpResponseMessage response = await client.GetAsync("city/dublin");

                HttpClient client = new HttpClient();
                var response =await DownloadClient.GetData(client, "city/dublin");

                // continue
                if (response.IsSuccessStatusCode)                                                   // 200.299
                {
                    // parse JSON data to City object
                    //SystemTray.ProgressIndicator.Text = "Downloading";
                    City city = await response.Content.ReadAsAsync<City>();
                    //string output = city.Description;

                    // assign textblocks with strings from the city object
                    CountryName.Text = city.Name;
                    DisplayText.Text = city.Description;

                    // turning off progress indicator
                    TestProgressBar.Visibility = System.Windows.Visibility.Collapsed;
                    //SetProgressIndicator(false);
                }
                else
                {
                    string output = response.StatusCode + " : " + response.ReasonPhrase;
                    throw new HttpRequestException(output);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
                TestProgressBar.Visibility = System.Windows.Visibility.Collapsed;
            }
            finally
            {
                // re-enable button
                GetDataButton.IsEnabled = true;
            }
        }

        // a helper method for the progress bar
        private static void SetProgressIndicator(bool isVisible)
        {
            SystemTray.ProgressIndicator.IsIndeterminate = isVisible;
            SystemTray.ProgressIndicator.IsVisible = isVisible;
        }

    }
}